<!--
    运维记录
 -->
<template>
    <div class="page">
        <div class="nuclein-query">
            <div>运维记录</div>
            <div style="margin-left: auto;">
                <el-button size="mini" type="primary" @click="handlerAdd()">添加</el-button>
            </div>
        </div>
        <!-- 表格 -->
        <div class="content-row">
            <el-table :data="tableData" style="width: 100%" border>
                <el-table-column label="设备ID" prop="deviceId">
                </el-table-column>
                <el-table-column label="运维记录名称" prop="operationsName">
                </el-table-column>
                <el-table-column label="设备更新时间" prop="deviceUpdateTime">
                </el-table-column>
                <el-table-column label="运维记录ID" prop="operationsId">
                </el-table-column>
                <el-table-column label="操作" align="center" width="160">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                            @click="handleDelete(scope.row.operationsId)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 20, 30, 40]" :page-size="size" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 弹出框 -->
        <div>
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="form">
                    <el-form-item label="设备id" :label-width="formLabelWidth">
                        <el-select v-model="form.deviceId" placeholder="请选择设备id">
                            <el-option v-for="item in deviceIdList" :key="item.deviceId" :label="item.deviceName"
                                :value="item.deviceId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="设备更新时间" :label-width="formLabelWidth">
                        <el-date-picker v-model="form.deviceUpdateTime" type="datetime" placeholder="选择设备更新时间">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="运维记录名称" :label-width="formLabelWidth">
                        <el-input v-model="form.operationsName" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="Submite">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { operationsLogsList, operationsLogsAddEdit, operationsLogsDelete, getEquipment } from '../../api/api'
import commonFunction from '../../utils/commonFunction'
import commonData from '../../utils/commonData'
export default {

  data () {
    return {
      title: '添加',
      dialogFormVisible: false,
      deviceLogType: commonData.deviceLogType,
      total: 10,
      page: 1,
      size: 10,
      formLabelWidth: '160px',
      tableData: [],
      deviceIdList: [],
      form: {
        // operationsId: '',        //  运维记录id
        deviceId: '', //  设备id
        deviceUpdateTime: '', //	设备更新的时间
        operationsName: '' //	运维记录名称
      }
    }
  },
  created () {
    this.gitList()
    this.getgetEquipment()
  },
  methods: {
    // 获取
    gitList () {
      const data = {
        page: this.page,
        size: this.size
      }
      operationsLogsList({ ...data }).then(res => {
        if (res.code == 0) {
          const { total, records } = res.page
          this.total = total
          this.tableData = records
          this.tableData = records.map(item => {
            const deviceUpdateTime = commonFunction.getTimeFormat(+new Date(item.deviceUpdateTime), 'YYYY-MM-DD hh:mm:ss')
            return {
              ...item,
              deviceUpdateTime
            }
          })
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    getgetEquipment () {
      getEquipment().then(res => {
        if (res.code === 0) {
          this.deviceIdList = res.list
        }
      })
    },
    // 添加
    handlerAdd () {
      this.form = {
        deviceId: '',
        deviceUpdateTime: '',
        operationsName: ''
      }
      this.title = '添加'
      this.dialogFormVisible = true
    },
    // 编辑
    handleEdit (row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      this.form = {
        operationsId: row.operationsId,
        deviceId: row.deviceId + '',
        deviceUpdateTime: row.deviceUpdateTime,
        operationsName: row.operationsName
      }
    },
    // 删除
    handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          operationsId: row,
          isDelete: true
        }
        operationsLogsDelete({ ...data }).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功')
            this.gitList()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 确认提交 添加、编辑
    Submite () {
      const data = {
        operationsId: this.form.operationsId ? this.form.operationsId : null,
        ...this.form
      }
      operationsLogsAddEdit(data).then(res => {
        if (res.code == 0) {
          this.$message.success(data.operationsId ? '编辑成功' : '添加成功')
          this.gitList()
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 分页
    handleSizeChange (val) {
      this.size = val
      this.gitList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.gitList()
    }
  }
}
</script>

<style scoped>
.page {
    position: relative;
    height: 100%;
}

.nuclein-query {
    display: flex;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #DCDFE6;
}

.content-row {
    overflow: hidden;
}

.device-type {
    padding: 20px 20px 10px 20px;
}

/* 分页 */
.pagination {
    width: 100%;
    padding: 20px;
    border-top: 1px solid #DCDFE6;
}
</style>
